package com.fer.tankr.models.enemies;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.fer.andenginemvc.api.gameObjects.GameObject;
import com.fer.tankr.models.Bullet;
import com.fer.tankr.models.Tank;

public abstract class Enemy extends GameObject<Rectangle> implements IUpdateHandler{

	private int lives;
	
	public Enemy(PhysicsWorld world, VertexBufferObjectManager vbom, float x, float y, int width, int height, int lives) {
		super(world, vbom, new Rectangle(x, y, width, height, vbom));
		this.lives = lives;
		this.entity.registerUpdateHandler(this);
	}
	
	

	@Override
	public Body initBody() {
		return PhysicsFactory.createBoxBody(physWorld, entity, BodyType.DynamicBody, fixtureDef);
	}

	@Override
	public FixtureDef initFixtureDef() {
		return PhysicsFactory.createFixtureDef(1, 1, 1);
	}
	
	public void reduceLives()
	{
		lives--;
		if (lives==0)
		{
			die();
		}
	}
	
	@Override
	public void onCollision(GameObject<?> go) {
		if (go instanceof Tank)
		{
			die();
		}
		else if (go instanceof Bullet)
		{
			reduceLives();
		}
	}
	
	@Override
	public void reset() {
		
	}

}
